﻿namespace PMS.Client.Controls
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    [ToolboxBitmap(typeof(SplitContainer))]
    public class UCSplitContainer : SplitContainer
    {
        private int _HeightOrWidth;
        private IContainer components = null;
        private bool mCollpased = false;
        private SplitterPanelEnum mCollpasePanel = SplitterPanelEnum.Panel1;
        private bool mIsSplitterFixed = true;
        private MouseState mMouseState = MouseState.Normal;
        private Rectangle mRect = new Rectangle();

        public UCSplitContainer()
        {
            base.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
            this.SplitterWidth = 9;
            this.Panel1MinSize = 0;
            this.Panel2MinSize = 0;
        }

        public void CollpaseOrExpand()
        {
            if (this.mCollpased)
            {
                this.mCollpased = false;
                base.SplitterDistance = this._HeightOrWidth;
            }
            else
            {
                this.mCollpased = true;
                this._HeightOrWidth = base.SplitterDistance;
                if (this.CollpasePanel == SplitterPanelEnum.Panel1)
                {
                    base.SplitterDistance = 0;
                }
                else if (base.Orientation == Orientation.Horizontal)
                {
                    base.SplitterDistance = base.Height - 9;
                }
                else
                {
                    base.SplitterDistance = base.Width - 9;
                }
            }
            base.Invalidate(this.ControlRect);
        }

        private Bitmap CreateControlImage(bool collapse, Color color)
        {
            int num;
            int num2;
            int num3;
            Bitmap bitmap = new Bitmap(80, 9);
            for (num = 5; num <= 30; num += 5)
            {
                num2 = 1;
                while (num2 <= 7)
                {
                    bitmap.SetPixel(num, num2, color);
                    num2 += 3;
                }
            }
            num = 50;
            while (num <= 0x4b)
            {
                num2 = 1;
                while (num2 <= 7)
                {
                    bitmap.SetPixel(num, num2, color);
                    num2 += 3;
                }
                num += 5;
            }
            if (collapse)
            {
                num3 = 0;
                for (num2 = 7; num2 >= 1; num2--)
                {
                    num = 0x23 + num3;
                    while (num <= (0x2d - num3))
                    {
                        bitmap.SetPixel(num, num2, color);
                        num++;
                    }
                    num3++;
                }
                return bitmap;
            }
            num3 = 0;
            for (num2 = 1; num2 <= 7; num2++)
            {
                for (num = 0x23 + num3; num <= (0x2d - num3); num++)
                {
                    bitmap.SetPixel(num, num2, color);
                }
                num3++;
            }
            return bitmap;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.components = new Container();
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            if (this.ControlRect.Contains(e.Location))
            {
                this.CollpaseOrExpand();
            }
            base.OnMouseClick(e);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            this.Cursor = Cursors.Default;
            this.mMouseState = MouseState.Normal;
            base.Invalidate(this.ControlRect);
            base.OnMouseLeave(e);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (base.SplitterRectangle.Contains(e.Location))
            {
                if (this.ControlRect.Contains(e.Location))
                {
                    if (!this.IsSplitterFixed)
                    {
                        this.IsSplitterFixed = true;
                        this.mIsSplitterFixed = false;
                    }
                    this.Cursor = Cursors.Hand;
                    this.mMouseState = MouseState.Hover;
                    base.Invalidate(this.ControlRect);
                }
                else
                {
                    if (!this.mIsSplitterFixed)
                    {
                        this.IsSplitterFixed = false;
                        if (base.Orientation == Orientation.Horizontal)
                        {
                            this.Cursor = Cursors.HSplit;
                        }
                        else
                        {
                            this.Cursor = Cursors.VSplit;
                        }
                    }
                    else
                    {
                        this.Cursor = Cursors.Default;
                    }
                    this.mMouseState = MouseState.Normal;
                    base.Invalidate(this.ControlRect);
                }
            }
            base.OnMouseMove(e);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            bool collapse = false;
            if (((this.CollpasePanel == SplitterPanelEnum.Panel1) && !this.mCollpased) || ((this.CollpasePanel == SplitterPanelEnum.Panel2) && this.mCollpased))
            {
                collapse = true;
            }
            Color color = (this.mMouseState == MouseState.Normal) ? SystemColors.ButtonShadow : SystemColors.ControlDarkDark;
            Bitmap image = this.CreateControlImage(collapse, color);
            if (base.Orientation == Orientation.Vertical)
            {
                image.RotateFlip(RotateFlipType.Rotate90FlipX);
            }
            e.Graphics.SetClip(base.SplitterRectangle);
            e.Graphics.Clear(this.BackColor);
            e.Graphics.DrawImage(image, this.ControlRect);
        }

        [DefaultValue(1)]
        public SplitterPanelEnum CollpasePanel
        {
            get
            {
                return this.mCollpasePanel;
            }
            set
            {
                if (value != this.mCollpasePanel)
                {
                    this.mCollpasePanel = value;
                    base.Invalidate(this.ControlRect);
                }
            }
        }

        private Rectangle ControlRect
        {
            get
            {
                if (base.Orientation == Orientation.Horizontal)
                {
                    this.mRect.X = (base.Width <= 80) ? 0 : ((base.Width / 2) - 40);
                    this.mRect.Y = base.SplitterDistance;
                    this.mRect.Width = 80;
                    this.mRect.Height = 9;
                }
                else
                {
                    this.mRect.X = base.SplitterDistance;
                    this.mRect.Y = (base.Height <= 80) ? 0 : ((base.Height / 2) - 40);
                    this.mRect.Width = 9;
                    this.mRect.Height = 80;
                }
                return this.mRect;
            }
        }

        public bool IsCollpased
        {
            get
            {
                return this.mCollpased;
            }
        }

        public bool IsSplitterFixed
        {
            get
            {
                return base.IsSplitterFixed;
            }
            set
            {
                base.IsSplitterFixed = value;
                if (!(!value || this.mIsSplitterFixed))
                {
                    this.mIsSplitterFixed = true;
                }
            }
        }

        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public int Panel1MinSize
        {
            get
            {
                return base.Panel1MinSize;
            }
            set
            {
                base.Panel1MinSize = 0;
            }
        }

        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public int Panel2MinSize
        {
            get
            {
                return base.Panel2MinSize;
            }
            set
            {
                base.Panel2MinSize = 0;
            }
        }

        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public int SplitterWidth
        {
            get
            {
                return base.SplitterWidth;
            }
            set
            {
                base.SplitterWidth = 9;
            }
        }

        private enum MouseState
        {
            Normal,
            Hover
        }

        public enum SplitterPanelEnum
        {
            Panel1,
            Panel2
        }
    }
}

